package org.jeecg.modules.demo.tobedone.controller;

import java.util.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.Constants;
import org.jeecg.modules.demo.annualkeywork.service.ITobedoneAnnualKeyWorkService;
import org.jeecg.modules.demo.tobedone.entity.ApprovalKpi;
import org.jeecg.modules.demo.tobedone.entity.ReportKpi;
import org.jeecg.modules.demo.tobedone.entity.Tobedone;
import org.jeecg.modules.demo.kpi.entity.kpistatusList;
import org.jeecg.modules.demo.kpi.service.IKpiStatusListService;
import org.jeecg.modules.demo.tobedone.service.IApprovalKpiService;
import org.jeecg.modules.demo.tobedone.service.IReportKpiService;
import org.jeecg.modules.demo.tobedone.service.ITobedoneService;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;

import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
 /**
 * @Description: 待办
 * @Author: jeecg-boot
 * @Date:   2025-08-07
 * @Version: V1.0
 */
@Tag(name="待办")
@RestController
@RequestMapping("/tobedone/tobedone")
@Slf4j

public class TobedoneController extends JeecgController<Tobedone, ITobedoneService> {
	@Autowired
	private ITobedoneService tobedoneService;
	@Autowired
	private IKpiStatusListService kpiStatusListService;
	@Autowired
	private IReportKpiService reportKpiService;
	@Autowired
	private IApprovalKpiService approvalKpiService;
	@Autowired
	private ITobedoneAnnualKeyWorkService tobedoneAnnualKeyWorkService;
	/**
	 * 分页列表查询
	 *
	 * @return
	 */
	//@AutoLog(value = "待办-分页列表查询")
	@Operation(summary="待办-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<kpistatusList>> queryPageList(
			@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
			@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
			HttpServletRequest req) {
/*
* 添加判断，待办功能只有在指标列表中存在该部门为配合部门才显示
* */
		PageHelper.startPage(pageNo, pageSize);
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<kpistatusList> kpistatusList = kpiStatusListService.SelectKpistatusListByOrgId(sysUser.getOrgId(),"待办");
		// 创建PageInfo对象
		PageInfo<kpistatusList> pageInfo = new PageInfo<>(kpistatusList);
		// 转换为IPage对象
		IPage<kpistatusList> page = new Page<>(pageNo, pageSize, pageInfo.getTotal());
		page.setRecords(pageInfo.getList());
		return Result.OK(page);
	}
	/**
	 *   添加
	 *
	 * @param tobedone
	 * @return
	 */
	@AutoLog(value = "待办-添加")
	@Operation(summary="待办-添加")
	@RequiresPermissions("tobedone:yx_tobedone:add")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody Tobedone tobedone) {
		tobedoneService.save(tobedone);
		return Result.OK(Constants.ADD_SUCCESS);
	}
	
	/**
	 *  编辑
	 *
	 * @param tobedone
	 * @return
	 */
	@AutoLog(value = "待办-编辑")
	@Operation(summary="待办-编辑")
	@RequiresPermissions("tobedone:yx_tobedone:edit")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody Tobedone tobedone) {
		tobedoneService.updateById(tobedone);
		return Result.OK(Constants.EDIT_SUCCESS);
	}
	
	/**
	 *   通过id删除
	 * @param id
	 * @return
	 */
	@AutoLog(value = "待办-通过id删除")
	@Operation(summary="待办-通过id删除")
	@RequiresPermissions("tobedone:yx_tobedone:delete")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		tobedoneService.removeById(id);
		return Result.OK(Constants.DELETE_SUCCESS);
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "待办-批量删除")
	@Operation(summary="待办-批量删除")
	@RequiresPermissions("tobedone:yx_tobedone:deleteBatch")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.tobedoneService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK(Constants.SOME_DELETE_SUCCESS);
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "待办-通过id查询")
	@Operation(summary="待办-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<Tobedone> queryById(@RequestParam(name="id",required=true) String id) {
		Tobedone tobedone = tobedoneService.getById(id);
		if(tobedone==null) {
			return Result.error(Constants.FIND_DATA_FAILURE);
		}
		return Result.OK(tobedone);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param tobedone
    */
    @RequiresPermissions("tobedone:yx_tobedone:exportXls")
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Tobedone tobedone) {
        return super.exportXls(request, tobedone, Tobedone.class, Constants.TODO);
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequiresPermissions("tobedone:yx_tobedone:importExcel")
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Tobedone.class);
    }
	/*
	接收任务
	* */
	@RequiresPermissions("tobedone:yx_tobedone:receiveTask")
	@Transactional//事务处理注解
	@RequestMapping(value = "/receiveTask", method = RequestMethod.POST)
	public Result<String> receiveTask() {
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		List<Tobedone> tobedoneList =tobedoneService.SelectTobedoneList();
		for (Tobedone t : tobedoneList) {
			if (t.getCoopDepart().equals(sysUser.getOrgId())) {
				// 根据kpi_id查询yx_tobedone表中的记录
				QueryWrapper<Tobedone> queryWrapper = new QueryWrapper<>();
				queryWrapper.eq("kpi_id", t.getKpiId());
				queryWrapper.eq("coop_depart", sysUser.getOrgId());
				Tobedone toUpdate = tobedoneService.getOne(queryWrapper);
				if (toUpdate != null) {
					toUpdate.setTaskstatus(Constants.RECEIVE);
					tobedoneService.updateById(toUpdate);
				}
			}
		}
        return Result.OK(Constants.RECEIVE_SUCCESS);
    }

	/*
	 * 驳回任务
	 * */

	 /*
	  * 审批年度关键指标
	  * */
	 @RequestMapping(value = "/approval_kpi", method = RequestMethod.GET)
	 public Result<IPage<ApprovalKpi>> approvalKpi(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
												   HttpServletRequest req) {
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 //指标的责任部门的上级
		 if(sysUser.getOrgId().equals(approvalKpiService.SelectApprovalKpiListByRespondepartment())) {
			 PageHelper.startPage(pageNo, pageSize);
			 List<ApprovalKpi> ApprovalKpiList = approvalKpiService.SelectApprovalKpiList();
			 // 创建PageInfo对象
			 PageInfo<ApprovalKpi> pageInfo = new PageInfo<>(ApprovalKpiList);
			 // 转换为IPage对象
			 IPage<ApprovalKpi> page = new Page<>(pageNo, pageSize, pageInfo.getTotal());
			 page.setRecords(pageInfo.getList());
			 return Result.OK(page);
		 }else{
			 return Result.OK(Constants.NO_DATA);
		 }
	 }

	 /**
	  *  审批按钮
	  *
	  * @param Approvalkpi
	  * @return
	  */
	 @RequiresPermissions("approval_kpi:yx_approval_kpi:approval")
	 @Transactional//事务处理注解
	 @RequestMapping(value = "/approval", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> approval(@RequestBody ApprovalKpi Approvalkpi) {
		 //将Approvalkpi中的approvalstatus改为已审批并存到result中去
		 //实现状态从待审批变为已审批
		 Date date = new Date();
		 Approvalkpi.setApprovalMonth(String.valueOf(date.getMonth()+1));
		 approvalKpiService.saveApprovalKpi(Approvalkpi);
		 //返回到前端的数据
		 return Result.OK(Constants.APPROVE_SUCCESS);
	 }
	 /*
	  * 汇报年度关键指标
	  * */
	 @RequestMapping(value = "/report_kpi", method = RequestMethod.GET)
	 public Result<IPage<ReportKpi>> reportKpi(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
											   HttpServletRequest req) {
		 PageHelper.startPage(pageNo, pageSize);
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 List<ReportKpi> ReportKpiList = reportKpiService.SelectReportKpiListAll(sysUser.getOrgId());
		 // 创建PageInfo对象
		 PageInfo<ReportKpi> pageInfo = new PageInfo<>(ReportKpiList);
		 // 转换为IPage对象
		 IPage<ReportKpi> page = new Page<>(pageNo, pageSize, pageInfo.getTotal());
//		 page.setRecords(pageInfo.getList());
		 return Result.OK(page);
	 }
	 /**
	  *  汇报按钮
	  *
	  * @param reportkpi
	  * @return
	  */
	 @RequiresPermissions("report_kpi:yx_report_kpi:report")
	 @Transactional//事务处理注解
	 @RequestMapping(value = "/reportaction", method = {RequestMethod.PUT,RequestMethod.POST})
	 public Result<String> reportaction(@RequestBody ReportKpi reportkpi) {
		 //实现状态从待汇报变为已汇报
		 reportKpiService.saveReportKpi(reportkpi);
		 return Result.OK(Constants.REPORT_SUCCESS);
	 }
	 /*
	 * 接收年度关键指标列表总数的记录
	 * */
	 @RequestMapping(value = "/ReceiveSumOfKpi",method = {RequestMethod.GET,RequestMethod.POST})
	 public int receiveSumOfKpi(){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 int sum=tobedoneService.ReceiveSumOfKpi(sysUser.getOrgId(),Constants.TODO);
		 System.out.println(sum);
		 Result.OK(sum);
		 return sum;
	 }
	 /*
	  * 汇报年度关键指标列表总数的记录
	  * */
	 @RequestMapping(value = "/ReportSumOfKpi",method = {RequestMethod.GET,RequestMethod.POST})
	 public int reportSumOfKpi(){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 int sum=reportKpiService.ReportSumOfKpi(sysUser.getOrgId());
		 System.out.println(sum);
		 Result.OK(sum);
		 return sum;
	 }
	 /*
	  * 审批年度关键指标列表总数的记录
	  * */
	 @RequestMapping(value = "/ApprovalSumOfKpi",method = {RequestMethod.GET,RequestMethod.POST})
	 public int approvalSumOfKpi(){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 if(sysUser.getOrgId().equals(approvalKpiService.SelectApprovalKpiListByRespondepartment())) {
			 int sum=approvalKpiService.ApprovalSumOfKpi();
			 System.out.println(sum);
			 Result.OK(sum);
			 return sum;
		 }else{
			 int sum=0;
			 Result.OK(sum);
			 return sum;
		 }
	 }
	/*年度关键指标列表的数据显示*/
	@RequestMapping(value = "/KpiSum",method = {RequestMethod.GET,RequestMethod.POST})
	public int kpiSum(){
		int sum=kpiStatusListService.KpiSum();
		System.out.println(sum);
		Result.OK(sum);
		return sum;
	}
	/*接收年度重点工作列表总数的记录*/
	@RequestMapping(value = "/TobedoneAnnualKeyWorkSum",method = {RequestMethod.GET,RequestMethod.POST})
	public int tobedoneAnnualKeyWorkSum(){
		LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		int sum=tobedoneAnnualKeyWorkService.TobedoneAnnualKeyWorkSum(sysUser.getOrgId());
		System.out.println(sum);
		Result.OK(sum);
		return sum;
	}
	 /*汇报年度重点工作列表总数的记录*/
	 @RequestMapping(value = "/ReportSumOfKeyWork",method = {RequestMethod.GET,RequestMethod.POST})
	 public int reportSumOfKeyWork(){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 int sum=tobedoneAnnualKeyWorkService.ReportSumOfKeyWork(sysUser.getOrgId());
		 System.out.println(sum);
		 Result.OK(sum);
		 return sum;
	 }
	 /*审批年度重点工作列表总数的记录*/
	 @RequestMapping(value = "/ApprovalSumOfKeyWork",method = {RequestMethod.GET,RequestMethod.POST})
	 public int approvalSumOfKeyWork(){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 int sum=tobedoneAnnualKeyWorkService.ApprovalSumOfKeyWork(sysUser.getOrgId());
		 System.out.println(sum);
		 Result.OK(sum);
		 return sum;
	 }
	 /*年度重点工作列表总数的记录*/
	 @RequestMapping(value = "/KeyWorkSum",method = {RequestMethod.GET,RequestMethod.POST})
	 public int keyWorkSum(){
		 LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
		 int sum=tobedoneAnnualKeyWorkService.KeyWorkSum(sysUser.getOrgId());
		 System.out.println(sum);
		 Result.OK(sum);
		 return sum;
	 }
}